package com.itheima.controller;

import com.itheima.pojo.PmsProductAttributeCategory;
import com.itheima.pojo.SmsFlashPromotion;
import com.itheima.service.PmsProductAttributeCategoryService;
import com.itheima.service.RoleService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.PmsProductAttributeCategoryVo;
import com.itheima.vo.PmsProductCategoryVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品类型
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity create(String name) {
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategoryService.create(pmsProductAttributeCategory);
        return ResponseEntity.ok(Result.success(null));
    }
    /**
     * 删除活动
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable("id") Integer id){
        pmsProductAttributeCategoryService.delete(id);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize){
        PageResultVo<PmsProductAttributeCategory> resultVo = pmsProductAttributeCategoryService.list(pageNum,pageSize);
        return ResponseEntity.ok(Result.success(resultVo));
    }


    /**
     * 修改商品属性
     * @param id
     * @param
     * @return
     */
    @PostMapping("update/{id}")
    public ResponseEntity update(@PathVariable("id") Integer id,
                                 String name){
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setName(name);
        pmsProductAttributeCategoryService.update(id,pmsProductAttributeCategory);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 获取单个属性
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") Integer id){
         PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.findById(id);
        return ResponseEntity.ok(Result.success(pmsProductAttributeCategory));
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/list/withAttr")
    public ResponseEntity withAttr() {
        List<PmsProductAttributeCategoryVo> vos = pmsProductAttributeCategoryService.withAttr();
        return ResponseEntity.ok(Result.success(vos));
    }
}
